//
//  DijkstraShortestPathV.swift
//  DataStructureWH
//
//  Created by kys-20 on 2019/11/2.
//  Copyright © 2019 王涵. All rights reserved.
//

import Foundation


class DijkstraShortestPathV : UIView{
    
      var textFiled = UITextField()
     var insterButton = UIButton()
    var labelCircular = [UILabel]()
    var cgPointArray = [CGPoint]()
    var cGPointArratAll = [CGPoint]()
    //贝塞尔曲线
    var pathArray = [UIBezierPath]()
    var shapeLayerArray = [CAShapeLayer]()
    var weightArray = [1,2,6,8,1,4,9,6,5,8]//权值
    var weightLabelArray = [UILabel]()
    var formLabelArray = [UILabel]()
    var titleLabelArray = [UILabel]()
    
    var oneArray = [0,1,2,3,0,4,6,5,1,4,7,8,5,2,7,9,9,6,3,8]
    var oneLabel = [1,2,4,5,7,8,10,11,13,14]
    var oneForm = [0,-1,1,0,2,0,5,1,7,2]
    
    var labelRightArray = [UILabel]()
    
    var AAAAra = ["0","01","02","013","024"]
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUpUi()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    
}

//MARK: - 动画操作
extension DijkstraShortestPathV{
    @objc func btnAction(){
        if textFiled.text != ""{
            redLineOne(int:0)
//            switch Int(textFiled.text!) {
//            case 0:
//
//            default:
//                print("error")
//            }
        }
    }
    
    
    func redLineOne(int:Int){
        for i in 0...3{
            self.shapeLayerArray[oneArray[int*4+i]].strokeColor = UIColor.red.cgColor
            self.shapeLayerArray[oneArray[int*4+i]].path = self.pathArray[oneArray[int*4+i]].cgPath
            
        }
        for i in 0...1{
             formLabelArray[oneLabel[int*2+i]].layer.borderWidth = 3
            formLabelArray[oneLabel[int*2+i]].layer.borderColor = UIColor.red.cgColor
            formLabelArray[oneLabel[int*2+i]].text = String(oneForm[int*2+i])
        }
        labelCircular[int].layer.borderColor = UIColor.red.cgColor
        
        
        
        sleep(3)
        UIView.animate(withDuration: 0.5, animations: {
            
        }) { (_) in
              for i in 0...3{
                self.shapeLayerArray[self.oneArray[int*4+i]].strokeColor = UIColor.black.cgColor
                self.shapeLayerArray[self.oneArray[int*4+i]].path = self.pathArray[self.oneArray[int*4+i]].cgPath
             
               }
            for i in 0...1{
                self.formLabelArray[self.oneLabel[int*2+i]].layer.borderWidth = 0.5
                    self.formLabelArray[self.oneLabel[int*2+i]].layer.borderColor = UIColor.black.cgColor
                   }
            self.labelCircular[int].layer.borderColor = UIColor.black.cgColor
          
            
            
            if int <= 3 {
                self.redLineOne(int:int+1)
            }else{
                self.Next(int:0)
            }
        }
    }
    func Next(int:Int){
        labelRightArray[int].text = String(AAAAra[int])
        sleep(2)
        UIView.animate(withDuration: 2, animations: {
            
        }) { (_) in
              if int <= 3 {
                self.Next(int: int+1)
            }
        }
        
    }
    
    
}

//MARK: - 创建初始视图
extension DijkstraShortestPathV{
      func setUpUi(){
            textFiled.frame = CGRect(x: 20, y: 90, width: 70, height: 40)
            textFiled.layer.borderColor = UIColor.red.cgColor
            textFiled.layer.borderWidth = 1
            self.addSubview(textFiled)
            insterButton.frame = CGRect(x: 100, y: 90, width: 70, height: 40)
            insterButton.backgroundColor = UIColor.white
            insterButton.setTitle("Run", for:.normal)
            insterButton.layer.borderWidth = 1
            insterButton.layer.borderColor = UIColor.black.cgColor
            insterButton.setTitleColor(UIColor.black, for: .normal)
            insterButton.setTitleColor(UIColor.brown, for: .highlighted)
                 insterButton.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
            self.addSubview(insterButton)

            setCircularLabel()
            calculationPoint()
            setLine()
            setWeightLabel()
            setFormLabelArray()
        }

        func setCircularLabel(){
            for i in 0...4{
                   let label = UILabel()
                   label.frame.size = CGSize(width: 40, height: 40)
                   label.text = String(i)
                   label.layer.borderWidth = 1
                   label.textAlignment = .center
                   label.layer.borderColor = UIColor.black.cgColor
                   label.layer.cornerRadius = 20
                   labelCircular.append(label)
                   self.addSubview(label)
               }
               
               labelCircular[0].center = CGPoint(x: UIScreen.main.bounds.width/2, y: 150)
               labelCircular[1].center = CGPoint(x: UIScreen.main.bounds.width/2-120, y: 230)
               labelCircular[2].center = CGPoint(x: UIScreen.main.bounds.width/2+120, y: 230)
               labelCircular[3].center = CGPoint(x: UIScreen.main.bounds.width/2-60, y: 340)
               labelCircular[4].center = CGPoint(x: UIScreen.main.bounds.width/2+60, y: 340)
        }
        func setWeightLabel(){
            for i in 0...9{
                let label = UILabel()
                label.backgroundColor = UIColor.clear
                label.textAlignment = .center
                label.textColor = UIColor.black
                label.frame.size = CGSize(width: 20, height: 20)
                label.center = CGPoint(x: (cGPointArratAll[2*i].x+cGPointArratAll[2*i+1].x)/2,
                                       y: (cGPointArratAll[2*i].y+cGPointArratAll[2*i+1].y)/2)
                label.text = String(weightArray[i])
                self.addSubview(label)
            }
        }
        func calculationPoint(){
               for i in labelCircular{
                   cgPointArray.append(i.center)
               }
            cGPointArratAll.append(cgPointArray[0])
            cGPointArratAll.append(cgPointArray[1])
            
            cGPointArratAll.append(cgPointArray[0])
            cGPointArratAll.append(cgPointArray[2])
            
            cGPointArratAll.append(cgPointArray[0])
            cGPointArratAll.append(cgPointArray[3])
            
            cGPointArratAll.append(cgPointArray[0])
            cGPointArratAll.append(cgPointArray[4])
            
            cGPointArratAll.append(cgPointArray[1])
            cGPointArratAll.append(cgPointArray[2])
            
            cGPointArratAll.append(cgPointArray[1])
            cGPointArratAll.append(cgPointArray[3])
            
            cGPointArratAll.append(cgPointArray[1])
            cGPointArratAll.append(cgPointArray[4])
            
            cGPointArratAll.append(cgPointArray[2])
            cGPointArratAll.append(cgPointArray[3])
            
            cGPointArratAll.append(cgPointArray[2])
            cGPointArratAll.append(cgPointArray[4])
            
            cGPointArratAll.append(cgPointArray[3])
            cGPointArratAll.append(cgPointArray[4])
           
        }
        func setFormLabelArray(){
            for i in 0...2{
                let label = UILabel()
                label.backgroundColor = UIColor.white
                label.frame = CGRect(x: 20+80*i, y: 380, width: 80, height: 30)
                if i == 0{
                    label.text = "Vertex"
                }else if i == 1{
                    label.text = "Cost"
                }else{
                    label.text = "Path"
                }
                label.textAlignment = .center
                label.textColor = UIColor.black
                self.addSubview(label)
            }
            for j in 0...4{
                for i in 0...2{
                    let label = UILabel()
                    label.tag = j*2+i
                    label.textAlignment = .center
                    if i == 0{
                        label.text = String(j)
                    }
                    label.layer.borderWidth = 0.5
                    label.layer.borderColor = UIColor.black.cgColor
                    label.frame = CGRect(x: 20+80*i, y: 410+j*30, width: 80, height: 30)
                    self.addSubview(label)
                    formLabelArray.append(label)
                }
            }
            for i in 0...4{
                let label = UILabel()
                label.textAlignment = .center
                
                label.frame = CGRect(x: 260, y: 410+i*30, width: 80, height: 30)
                self.addSubview(label)
                labelRightArray.append(label)
            }
           
        }
        func setLine(){
            for i in 0...cGPointArratAll.count-1{
                if i%2 == 0{
                    let path = UIBezierPath()
                    let shapeLayer = CAShapeLayer()
                    path.move(to: cGPointArratAll[i])
                    path.addLine(to: cGPointArratAll[i+1])
                    path.close()
                    shapeLayer.strokeColor = UIColor.black.cgColor
                    shapeLayer.fillColor = UIColor.clear.cgColor
                    shapeLayer.lineWidth = 1
                    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
                    shapeLayer.lineCap = CAShapeLayerLineCap.round
                    shapeLayer.path = path.cgPath
                    shapeLayer.strokeStart = 0
                    shapeLayer.strokeEnd = 1
                    self.layer.addSublayer(shapeLayer)
                    shapeLayerArray.append(shapeLayer)
                    pathArray.append(path)
                    shapeLayer.path = path.cgPath
                }
                
            
            }
        }
        
}
